//
//  BandViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/12.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "BandViewController.h"
#import "BandCell.h"
#import "AddBandCardViewController.h"
#import "BandCardStore.h"
#import "BankCarList.h"
#import "BandCardModel.h"
#import "SetDefaultBankCardViewController.h"

@interface BandViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *listArr;

@property (nonatomic,strong) UIView *footerView;

@end

@implementation BandViewController


- (UIView *)footerView{
    
    if (!_footerView) {
        
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,ScreenWidth, 60)];
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, 50)];
        [btn setBackgroundColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 0);
        [btn addTarget:self action:@selector(onBtn) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"+ 添加银行卡" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerView addSubview:btn];
    }
    
    return _footerView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 0.5;
        _tableView.sectionFooterHeight = 9.5;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        _tableView.tableFooterView = self.footerView;
        [self.view addSubview:self.tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(0);
            
        }];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf loadData];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        
        
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#333333"];
    
    [self setBavBar];
    
    [self.tableView.mj_header beginRefreshing];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:@"addSuccess" object:nil];
}

- (void)setBavBar{
    
    [self setNavBarWithTitle:@"银行卡"];
    
    UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //修改按钮向右偏移10 point
    [settingButton setFrame:CGRectMake(18.0, 0.0, 44.0, 44.0)];
    [settingButton addTarget:self action:@selector(onSet:) forControlEvents:UIControlEventTouchUpInside];
    [settingButton setImage:[UIImage imageNamed:@"设置"] forState:UIControlStateNormal];
    //修改方法
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];
    [view addSubview:settingButton];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)onSet:(UIButton *)sender{
    
    SetDefaultBankCardViewController *VC = [[SetDefaultBankCardViewController alloc] init];
    MJWeakSelf
    [VC returnBlock:^{
        
        [weakSelf loadData];
    }];
    VC.listArr = self.listArr;
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 刷新
- (void)loadData{
    
    BandCardStore *store = [[BandCardStore alloc] init];
    
    MJWeakSelf
    [store getBandCardListSuccess:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        weakSelf.listArr = [NSMutableArray arrayWithArray:listArr];
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BandCell getHeight];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BandCell *cell = [BandCell tempWithTableView:tableView];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    cell.model = self.listArr[indexPath.section];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [BankCarList sharedInstance].selectBandModel = self.listArr[indexPath.section];
    
    if (self.tixian) {
        
        if (self.myblock!=nil) {
            
            self.myblock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BandCardModel *model = self.listArr[indexPath.section];
    
    if ([model.defaultCard intValue] == 1) {
        
        return UITableViewCellEditingStyleNone;
    }
    return UITableViewCellEditingStyleDelete;
}

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    // delete action
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"删除", @"") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        [self deleteBankCardWithIndexPath:indexPath];

    }];

    deleteAction.backgroundColor = BLUECOLOR;

//    UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:NSLocalizedString(@"取消", @"") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
//
//        [tableView setEditing:NO animated:YES];
//
//    }];

    return @[deleteAction];

}

#pragma mark - 删除银行卡
- (void)deleteBankCardWithIndexPath:(NSIndexPath *)indexPath{

    BandCardModel *model = self.listArr[indexPath.section];
    if ([model.defaultCard intValue] == 1) {

        return [self showMBPError:@"默认银行卡不可删除"];
    }

    MJWeakSelf
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您将删除此银行卡" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        [weakSelf.tableView setEditing:NO animated:YES];

    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        BandCardStore *store = [[BandCardStore alloc] init];
        [store deleteBankCardWithId:model.uid Success:^{

            [weakSelf.tableView setEditing:NO animated:YES];

//            [weakSelf refresh];

            [weakSelf.listArr removeObjectAtIndex:indexPath.section];

            [weakSelf.tableView beginUpdates];

            [weakSelf.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]withRowAnimation:UITableViewRowAnimationLeft];

            [weakSelf.tableView endUpdates];


            [weakSelf showMBPError:@"删除成功！"];

            [weakSelf.tableView reloadData];

        } Failure:^(NSError *error) {


            [weakSelf showMBPError:[HttpTool handleError:error]];
        }];



    }]];
    [self presentViewController:alert animated:YES completion:^{

    }];

}



-(void)onBtn{
    
    DLog(@"添加银行卡");
    AddBandCardViewController *VC = [[AddBandCardViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    
}

- (void)returnMyBlock:(myblock)block{
    
    self.myblock = block;
}


@end
